#!/bin/env bash

# SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
#
# SPDX-License-Identifier: LGPL-3.0-or-later

# linglong cli completion

__ll_cli_find_subcommand() {
        lines=$(ll-cli --help | wc -l)
        ll-cli --help | grep "Subcommands:" -A $lines | tail -n+2 | awk -F\  '{print $1}' | tr '\n' ' '
}

__ll_cli_get_container_list() {
        ll-cli ps | tail -n+2 | awk '{print $1}' | tr '\n' ' '
}

__ll_cli_get_installed_list() {
        ll-cli list | tail -n+2 | awk '{print $1}' | tr '\n' ' '
}

__ll_cli_get_app_list() {
        ll-cli search . | tail -n+2 | awk '{print $1}' | tr '\n' ' '
}

__ll_cli_get_layer_list() {
        ls $1* 2>/dev/null | tr '\n' ' '
}

_ll_cli() {
        local wordlist

        common_option="--version --json --help -h"
        if [[ "${#COMP_WORDS[@]}" == "2" ]]; then
                wordlist=$(__ll_cli_find_subcommand)
        fi
        wordlist="$wordlist $common_option"

        case "${COMP_WORDS[1]}" in
        run)
                run_option="--no-dbus-proxy --dbus-proxy-cfg"
                wordlist="$wordlist $run_option $(__ll_cli_get_installed_list)"
                ;;
        ps)
                wordlist=$wordlist
                ;;
        exec)
                exec_option="--working-directory"
                wordlist="$wordlist $exec_option $(__ll_cli_get_container_list)"
                ;;
        enter)
                enter_option="--working-directory"
                wordlist="$wordlist $enter_option $(__ll_cli_get_container_list)"
                ;;
        kill)
                wordlist="$wordlist $(__ll_cli_get_container_list)"
                ;;
        install)
                install_option="--no-dbus"
                wordlist="$wordlist $install_option $(__ll_cli_get_app_list) $(__ll_cli_get_layer_list ${COMP_WORDS[2]})"
                ;;
        uninstall)
                uninstall_option="--all --prune"
                wordlist="$wordlist $uninstall_option $(__ll_cli_get_installed_list)"
                ;;
        upgrade)
                wordlist="$wordlist $(__ll_cli_get_installed_list)"
                ;;
        search)
                search_option="--type"
                wordlist="$wordlist $search_option $(__ll_cli_get_app_list)"
                ;;
        list)
                list_option="--no-dbus --type"
                wordlist="$wordlist $list_option"
                ;;
        repo)
                subcommand="modify list"
                repo_option=""
                if [[ "${COMP_WORDS[2]}" == "modify" ]]; then
                        subcommand=""
                        repo_option="--name"
                elif [[ "${COMP_WORDS[2]}" == "show" ]]; then
                        subcommand=""
                        repo_option=""
                fi
                wordlist="$wordlist $subcommand $repo_option"
                ;;
        info)
                wordlist="$wordlist $(__ll_cli_get_installed_list) $(__ll_cli_get_layer_list ${COMP_WORDS[2]})"
                ;;
        esac

        local cur=${COMP_WORDS[COMP_CWORD]}
        COMPREPLY=($(compgen -W "${wordlist}" -- "${cur}"))

        return 0
}

complete -F _ll_cli ll-cli
complete -F _ll_cli llpkg
